package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.ToolbarBaseActivity;
import com.xmut.bs.handaccount.bmob.User;
import com.xmut.bs.handaccount.util.DateUtils;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.xmut.bs.handaccount.util.StringUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

public class PersonInfoActivity extends ToolbarBaseActivity {


    @BindView(R.id.user_name_tv)
    TextView mUserNameTv;
    @BindView(R.id.head_iv)
    ImageView mHeadIv;
    @BindView(R.id.sex_tv)
    TextView mSexTv;
    @BindView(R.id.birthday_tv)
    TextView mBirthdayTv;

    private final int REQUEST_BIRTHDAY = 0x01;
    private final int REQUEST_SEX = 0x02;
    private final int REQUEST_NICK_NAME = 0x03;
    private final int REQUEST_SIGN = 0x04;
    public static final int REQUEST_CODE_CHOOSE_HEAD_GALLERY = 0x05;
    public User mUser;

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_person_info_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mUser = BmobUser.getCurrentUser(User.class);
        String user_nickname = mUser.getNickName();
        mUserNameTv.setText(StringUtil.isNull(user_nickname) ? "未设置昵称" : user_nickname);
        GlideApp.with(this)
                .load(mUser.getAvatar())
                .fitCenter()
                .error(R.mipmap.ic_head_img)
                .placeholder(R.mipmap.ic_head_img)
                .transform(new GlideCircleTransform(mContext))
                .into(mHeadIv);
        mSexTv.setText(mUser.getSex() == null ? "保密" : getSex(mUser.getSex()));
        mBirthdayTv.setText(mUser.getBirthday() == null ? "保密" : DateUtils.DateToString(DateUtils.StringToDateWithSprit(mUser.getBirthday().getDate())));
    }

    @Override
    protected void initParams(Intent intent) {

    }

    @Override
    protected String getSubTitle() {
        return "个人中心";
    }

    @OnClick({R.id.user_name_tv, R.id.head_iv, R.id.sex, R.id.birthday, R.id.sign, R.id.login_out_btn, R.id.reset_password})
    public void onViewClicked(View view) {
        File file = new File("temp.jpg");
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.user_name_tv:
                intent.putExtra(EditPersonInfoActivity.PARAM_TYPE, EditPersonInfoActivity.PARAM_NICK);
                startActivityForResult(EditPersonInfoActivity.class, intent, REQUEST_NICK_NAME);
                break;
            case R.id.head_iv:
                startActivityForResult(BGAPhotoPickerActivity.newIntent(this,
                        file, 1, null, false),
                        REQUEST_CODE_CHOOSE_HEAD_GALLERY);
                break;
            case R.id.sex:
                intent.putExtra(EditPersonInfoActivity.PARAM_TYPE, EditPersonInfoActivity.PARAM_SEX);
                startActivityForResult(EditPersonInfoActivity.class, intent, REQUEST_SEX);
                break;
            case R.id.birthday:
                intent.putExtra(EditPersonInfoActivity.PARAM_TYPE, EditPersonInfoActivity.PARAM_BIRTHDAY);
                startActivityForResult(EditPersonInfoActivity.class, intent, REQUEST_BIRTHDAY);
                break;
            case R.id.sign:
                intent.putExtra(EditPersonInfoActivity.PARAM_TYPE, EditPersonInfoActivity.PARAM_SIGNATURE);
                startActivityForResult(EditPersonInfoActivity.class, intent, REQUEST_SIGN);
                break;
            case R.id.reset_password:
                break;
            case R.id.login_out_btn:
                BmobUser.logOut();
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String value = data.getStringExtra(EditPersonInfoActivity.PARAM_VALUE);
            switch (requestCode) {
                case REQUEST_BIRTHDAY:
                    mBirthdayTv.setText(value);
                    break;
                case REQUEST_SEX:
                    mSexTv.setText(getSex(Integer.valueOf(value)));
                    break;
                case REQUEST_NICK_NAME:
                    mUserNameTv.setText(value);
                    break;
                case REQUEST_SIGN:
//                    mSignTv.setText(value);
                    break;
                case REQUEST_CODE_CHOOSE_HEAD_GALLERY:
                    picturePath = BGAPhotoPickerActivity.getSelectedImages(data).get(0);
                    final BmobFile bmobFile = new BmobFile(new File(picturePath));
                    bmobFile.uploadblock(new UploadFileListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                //bmobFile.getFileUrl()--返回的上传文件的完整地址
//                                showToast("上传文件成功:" + bmobFile.getFileUrl());
                                GlideApp.with(mHeadIv)
                                        .load(bmobFile.getFileUrl())
                                        .fitCenter()
                                        .error(R.mipmap.ic_head_img)
                                        .placeholder(R.mipmap.ic_head_img)
                                        .transform(new GlideCircleTransform(mContext))
                                        .into(mHeadIv);
                                mUser.setAvatar(bmobFile.getFileUrl());
                                mUser.update(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e == null) {
                                            showToast("头像保存成功");
                                        } else {
                                            showToast("头像保存失败：" + e.getMessage());
                                        }
                                    }
                                });
                            } else {
                                showToast("上传头像失败：" + e.getMessage());
                            }
                        }

                        @Override
                        public void onProgress(Integer value) {
                            // 返回的上传进度（百分比）
                        }
                    });
                    break;
            }
        }
    }

    private String picturePath;

    public String getSex(Integer sex) {
        switch (sex) {
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "保密";
        }
    }
}
